
#include<lsl-util/license.lsl>

#include<lsl-util/lnc-0.9.lsl>
#include<lsl-util/http_request-0.1.lsl>

// URL of php script server
//string site_url = "http://www.secondlifelab.it/rss.php";

// URL of RSS to read
string url2get = "http://www.secondlifelab.it/index.php?option=com_rss&feed=RSS2.0&no_html=1";

string version = "1.0.0";

integer link;

// number of news to view
integer number_of_news;

// lista contenente la cache
list newsCache_text;
list newsCache_number;

//integer timeout;

// Function for encode the url of RSS to send at the phph script
string encode_url(string url){
    list chars = ["&","?","=","#","$"];
    list chars_enc = ["$1","$2","$3","$4","$5"];

    list lista = llParseString2List($url,[],chars);

    integer i;
    string url_enc = "";
    for(i=0; i<llGetListLength(lista); i++){
        string element = llList2String(lista,i);

        integer n;
        if((n = llListFindList(chars,[element])) != -1){
            url_enc += llList2String(chars_enc,n);
        }else{
            url_enc += element;
        }
    }

    return url_enc;
}

default{

    state_entry(){
	// Set "touch text" to "Read News"
        llSetTouchText("Read News");

	// set name of notecard
	LNC_name = "NotecardInfo";

	// azzero la cache
	newsCache_text = [];
	newsCache_number = [];

	// read notecard
	state LNC_in;

    }

    // On REZ reset all script.
    on_rez(integer start_param) {
        llResetScript();
    }

    changed(integer change){
    	if (change & CHANGED_INVENTORY) {
		// probabilmente, e' stata modificata la notecard
		llResetScript();
	}
    }

}

state LNC_out{
	state_entry(){
		// RSS da controllare
		url2get  = LNCgetVar("url2get");

		// server php che fa da filtro
		HRP_url = LNCgetVar("site_url");

		// number of news to view
		number_of_news = (integer)LNCgetVarD("number_of_news", "5");

		// set timeout for reloading the titles and reset cache
		//timeout = (integer)LNCgetVarD("timeout", "500");
		// manda un msg di reset a tutti gli script
		llMessageLinked(LINK_ALL_OTHERS, 0, "", NULL_KEY);

		// start!
		state first;
	}
}

state first{
    state_entry(){
    	//llSetTimerEvent(timeout);

	// Send a msg to root for request the list of title
	//llMessageLinked(LINK_ROOT, 2, "", NULL_KEY);
    }

    // sender_num is the link number of the sending object in the set.
    // link_number - children che ha fatto la richiesta
    link_message(integer sender_num, integer link_number, string msg, key id) {
        link = link_number;

	integer number = link_number - 3;
		
	// reset param
	HRPresetHttpGet();

        if(link == 2){
		if(msg == "version"){
		    // Get version
		    HRPaddHttpGet("op","version");
		}else{
		    // Get the titles
		    HRPaddHttpGet("op","titles");
		    HRPaddHttpGet("number",(string)number_of_news);
		}
        }else{// devo leggere una nuova news

	    integer index;
   	    // guardo se ho gia' questa news nella cache
	    if((index = llListFindList( newsCache_number, [number] )) != -1){
	    	// la news e' gia' nella cache
		llMessageLinked(LINK_ALL_OTHERS, link, llList2String(newsCache_text, index), NULL_KEY);
	
		// fine
		return;
	    }

	    // Get the body of a news
	    HRPaddHttpGet("op","body");
	    HRPaddHttpGet("number",(string)number);
        }

	// Send Region Name and Pos
	HRPaddHttpGet("region",llGetRegionName());

	// Send position of myrss
	vector pos = llGetPos();
	HRPaddHttpGet("coo_x",(string)pos.x);
	HRPaddHttpGet("coo_y",(string)pos.y);
	HRPaddHttpGet("coo_z",(string)pos.z);

	// Send version of myrss
	HRPaddHttpGet("version",version);

	// url of rss
	HRPaddHttpGet("url",encode_url(url2get));

	// start request
	state HRP_in;
    }

    // On REZ reset all script.
    on_rez(integer start_param) {
        llResetScript();
    }
	
    changed(integer change){
    	if (change & CHANGED_INVENTORY) {
		// probabilmente, e' stata modificata la notecard
		llResetScript();
	}
    }

    timer(){
    	llResetScript();
    }
}

state HRP_out{

    state_entry(){
    	if(HRP_status == 200){
		if(link == 2){
			// svuota la cache
			newsCache_text = [];
			newsCache_number = [];
		}else{
			// salvo la news in cache
			newsCache_text   += [HRP_body];
			newsCache_number += [link - 3];
		}

		// manda la news
		llMessageLinked(LINK_ALL_OTHERS, link, HRP_body, NULL_KEY);
	}else if(HRP_status != 0){
		// si e' verificato qualche errore, resetta tutto!
		llMessageLinked(LINK_ALL_OTHERS, -1, "", NULL_KEY);
	}

	// aspetta un nuovo comando
	state first;

    }

    changed(integer change){
    	if (change & CHANGED_INVENTORY) {
		// probabilmente, e' stata modificata la notecard
		llResetScript();
	}
    }

    timer(){
    	llResetScript();
    }

}
